package com.android.equipment.ui.model

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.android.equipment.R
import com.android.equipment.adapter.DeviceModelManagerAdapter
import com.android.equipment.base.BaseActivity
import com.android.equipment.dao.DeviceModel
import com.android.equipment.dao.operate.DaoDeviceModel
import com.android.equipment.databinding.ActivityDeviceModelManagerBinding
import com.android.equipment.ui.UpdateConfigActivity
import com.gyf.immersionbar.ImmersionBar
import com.hjq.bar.OnTitleBarListener
import com.lxj.xpopup.XPopup

class DeviceModelManagerActivity : BaseActivity<ActivityDeviceModelManagerBinding>() {
    private lateinit var modelManagerAdapter: DeviceModelManagerAdapter
    private var modelList: MutableList<DeviceModel>? = null
    override fun setLayoutContent(): ActivityDeviceModelManagerBinding {
        return ActivityDeviceModelManagerBinding.inflate(layoutInflater)
    }

    override fun initView(savedInstanceState: Bundle?) {
        initRecycler()
    }

    override fun initData() {
        val notDelDeviceModel = DaoDeviceModel.getInstance().getNotDelDeviceModel()
        modelManagerAdapter.setList(notDelDeviceModel)
    }

    override fun initEvent() {
        super.initEvent()
        binding.mTitleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(v: View) {
                mSwipeBackHelper.backward()
            }

            override fun onTitleClick(v: View) {
            }

            override fun onRightClick(v: View) {

            }
        })
    }

    private fun initRecycler() {
        modelManagerAdapter = DeviceModelManagerAdapter(R.layout.item_device_model_manager)
        binding.rvDeviceModelManager.adapter = modelManagerAdapter
        modelManagerAdapter.setOnItemClickListener { adapter, view, position ->
            val deviceMode = modelManagerAdapter.getItem(position)
            val intent = Intent(this, EditDeviceModelActivity::class.java)
            intent.putExtra("id", deviceMode.id)
            mSwipeBackHelper.forward(intent)
        }

        modelManagerAdapter.addChildClickViewIds(R.id.tv_update_config)
        modelManagerAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tv_update_config) {
                val intent = Intent(this, UpdateConfigActivity::class.java)
                intent.putExtra("deviceModelId", modelManagerAdapter.getItem(position).id)
                mSwipeBackHelper.forward(intent)
            }
        }
    }


    private fun refreshData() {
        modelList = DaoDeviceModel.getInstance().notDelDeviceModel
        modelList?.let {
            modelManagerAdapter.setList(it)
        }
    }

    override fun setStatusBar() {
        super.setStatusBar()
        ImmersionBar.with(this)
            .fullScreen(true)
            .statusBarColor(R.color.F8F9FA)
            .statusBarDarkFont(true)
            .titleBar(binding.mTitleBar)
            .init()
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }
}